package com.common.utils;

import com.alibaba.fastjson.JSONObject;

import java.net.*;
import java.util.List;
import java.util.Map;

import static com.alibaba.druid.util.Utils.read;

/**
 * @类名 IDCardUtils
 * @描述 TODO
 * @版本 1.0
 * @创建人 Lying~
 * @创建时间 2022/1/19 19:28
 * @修改人 Lying~
 * @修改时间 2022/1/19 19:28
 **/
public class IDCardUtils {


    public static String bankCardVerification(String userName, String phoneNumber, String idCardCode, String bankAccountNo) {
        String msg = "";
        String host = "https://b4bankcard.market.alicloudapi.com";// 【1】请求地址 支持http 和 https 及 WEBSOCKET
        String path = "/bankCheck4New";  // 【2】后缀
        String appcode = "49701eef55b740dcbcb5c9193ef62b76"; // 【3】开通服务后 买家中心-查看AppCode
        String idCard = idCardCode;// 身份证
        String name = userName;// 真实姓名
        String accountNo = bankAccountNo;// 银行卡号
        String mobile = phoneNumber;// 手机号
        try {
            String urlSend = host + path + "?idCard=" + idCard + "&name=" + URLEncoder.encode(name, "UTF-8") + "&accountNo=" + accountNo + "&mobile=" + mobile;  // 【5】拼接请求链接
            URL url = new URL(urlSend);
            HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
            httpURLCon.setRequestProperty("Authorization", "APPCODE " + appcode);// 格式Authorization:APPCODE (中间是英文空格)
            int httpCode = httpURLCon.getResponseCode();
            if (httpCode == 200) {
                String json = read(httpURLCon.getInputStream());
                JSONObject jsonObject = JSONObject.parseObject(json);
                //验证通过
                System.out.print(jsonObject.get("msg"));
                msg = jsonObject.get("msg").toString();
                System.out.println("获取返回的json:");
                System.out.print(json);
            } else {
                Map<String, List<String>> map = httpURLCon.getHeaderFields();
                String error = map.get("X-Ca-Error-Message").get(0);
                if (httpCode == 400 && error.equals("Invalid AppCode `not exists`")) {
                    System.out.println("AppCode错误 ");
                } else if (httpCode == 400 && error.equals("Invalid Url")) {
                    System.out.println("请求的 Method、Path 或者环境错误");
                } else if (httpCode == 400 && error.equals("Invalid Param Location")) {
                    System.out.println("参数错误");
                } else if (httpCode == 403 && error.equals("Unauthorized")) {
                    System.out.println("服务未被授权（或URL和Path不正确）");
                } else if (httpCode == 403 && error.equals("Quota Exhausted")) {
                    System.out.println("套餐包次数用完 ");
                } else if (httpCode == 403 && error.equals("Api Market Subscription quota exhausted")) {
                    System.out.println("套餐包次数用完，请续购套餐");
                } else {
                    System.out.println("参数名错误 或 其他错误");
                    System.out.println(error);
                }
            }

        } catch (MalformedURLException e) {
            System.out.println("URL格式错误");
        } catch (UnknownHostException e) {
            System.out.println("URL地址错误");
        } catch (Exception e) {
            // 打开注释查看详细报错异常信息
            // e.printStackTrace();
        }
        return msg;
    }

    public static void main(String[] args) {
        String s = bankCardVerification("刘勇", "13530448368", "421223199410033872", "6214832746717995");
        System.out.println(s);
    }


}